package com.ctjsoft.fileupload.controller;

import com.ctjsoft.fileupload.entity.ReturnMessage;
import com.ctjsoft.fileupload.entity.TBucketServer;
import com.ctjsoft.fileupload.entity.TFile;
import com.ctjsoft.fileupload.entity.TMinioServer;
import com.ctjsoft.fileupload.mapper.TFileMapper;
import com.ctjsoft.fileupload.server.TBucketServerService;
import com.ctjsoft.fileupload.server.TFileService;
import com.ctjsoft.fileupload.server.TMinioServerService;
import com.ctjsoft.fileupload.utils.MinioClientUtils;
import com.ctjsoft.fileupload.utils.SpringUtil;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther chen1
 * @create 2020-08-03 23:19
 */
@RestController("/file")
@Log4j2
@Api(tags = "文件控制器")
public class FileController {

    @Autowired
    TMinioServerService tMinioServerService;

    @Autowired
    TBucketServerService tBucketServerService;

    @Autowired
    TFileService tFileService;

    @PostMapping("upload")
    @ApiOperation(value = "文件上传")
    public ReturnMessage uploadFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "serverName", required = false) String serverName) throws IOException, InvalidKeyException, InvalidResponseException, RegionConflictException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        ReturnMessage returnMessage = new ReturnMessage();
        Map<String, MinioClientUtils> minioClientList = (Map) SpringUtil.getBean("minioClientList");
        String etag = null;
        log.info("文件上传，服务名：" + serverName);
        ObjectWriteResponse objectWriteResponse = null;
        if (StringUtils.isNotEmpty(serverName)) {
            //获取连接信息
            //TMinioServer minioServer = tMinioServerService.getMinioServerByName(serverName);
            //获取桶信息
            TBucketServer bucketServer = tBucketServerService.getBucketByName(serverName);
            MinioClientUtils minioClientUtils = null;

            //f (minioServer != null) {
            if (minioClientList != null) {
                minioClientUtils = minioClientList.get(serverName);
            }
            //MinioClientUtils minioClientUtils = new MinioClientUtils(minioServer);
            if (bucketServer != null) {
                objectWriteResponse = minioClientUtils.putObject(file, bucketServer.getBucketName());
            }
            //}
            if (objectWriteResponse != null) {
                //文件信息写入库中
                TFile tFile = new TFile();
                tFile.setId(objectWriteResponse.etag());
                tFile.setBucketId(bucketServer.getId());
                tFile.setContentType(file.getContentType());
                tFile.setFileName(file.getOriginalFilename());
                tFile.setUploadTime(new Date());
                int i = tFileService.addFile(tFile);
                etag = objectWriteResponse.etag();
                returnMessage.setMsg("文件上传状态：" + (i > 0 ? "上传成功" : "上传失败") + "，标签ID:" + etag);
            }
            HashMap<Object, Object> map = new HashMap<>();
            //将文件标签作为ID，返回给调用者
            map.put("fileId", etag);
            returnMessage.setData(map);
        }
        //}
        return returnMessage;
    }
}
